/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 *
 * @format
 * @flow strict-local
 */

import codePush from 'react-native-code-push';
const codePushOptions = {checkFrequency: codePush.CheckFrequency.ON_APP_RESUME};

import React, {Component} from 'react';
import {
  StyleSheet,
  Dimensions,
  View,
  Text,
  TouchableOpacity,
} from 'react-native';

const windowWidth = Dimensions.get('window').width; //设备的宽度

class App extends Component {
  clickToCheckUpdate() {
    codePush.sync({
      //安装模式
      //ON_NEXT_RESUME 下次恢复到前台时
      //ON_NEXT_RESTART 下一次重启时
      //IMMEDIATE 马上更新
      installMode: codePush.InstallMode.IMMEDIATE,
      //对话框
      updateDialog: {
        //是否显示更新描述
        appendReleaseDescription: true,
        //更新描述的前缀。 默认为"Description"
        descriptionPrefix: '更新内容：',
        //强制更新按钮文字，默认为continue
        mandatoryContinueButtonLabel: '立即更新',
        //强制更新时的信息. 默认为"An update is available that must be installed."
        mandatoryUpdateMessage: '必须更新后才能使用',
        //非强制更新时，按钮文字,默认为"ignore"
        optionalIgnoreButtonLabel: '稍后',
        //非强制更新时，确认按钮文字. 默认为"Install"
        optionalInstallButtonLabel: '后台更新',
        //非强制更新时，检查到更新的消息文本
        optionalUpdateMessage: '有新版本了，是否更新？',
        //Alert窗口的标题
        title: '更新提示',
      },
    });
  }
  render() {
    return (
      <View style={styles.sectionContainer}>
        <Text
          onPress={() => this.clickToCheckUpdate()}
          style={styles.sectionTitle}>
          当前版本5
        </Text>
        <TouchableOpacity
          style={styles.touch}
          onPress={() => this.clickToCheckUpdate()}>
          <Text style={styles.checkText}>检查更新</Text>
        </TouchableOpacity>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  sectionContainer: {
    width: windowWidth,
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
  sectionTitle: {
    fontSize: 24,
    fontWeight: '600',
    color: 'black',
  },
  touch: {
    width: windowWidth - 100,
    height: 34,
    borderRadius: 4,
    backgroundColor: '#1b2fcc',
    justifyContent: 'center',
    alignItems: 'center',
    marginTop: 40,
  },
  checkText: {
    fontSize: 14,
    color: 'white',
  },
});

export default codePush(codePushOptions)(App);
